/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.just.dusin.ooii.component.kj;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.just.dusin.ooii.database.CLCSBean;
import org.just.dusin.ooii.database.CLYCBean;
import org.just.dusin.ooii.database.DataOf5mBean;
import org.just.dusin.ooii.database.MineData;
import org.just.dusin.ooii.database.OrgDao4SQLServer;
import org.just.dusin.ooii.database.QHSSSJBean;
import org.just.dusin.ooii.util.DateUtil;

/**
 *
 * @author Administrator
 */
public class DataPackage {
    
    private static OrgDao4SQLServer orgDao;

    public static OrgDao4SQLServer getOrgDao() {
        return orgDao;
    }

    public static void setOrgDao(OrgDao4SQLServer orgDao) {
        DataPackage.orgDao = orgDao;
    }
    
    /**
     * 包装仪表数据
     * no表示设备编号
     * type表示类型，type=1表示仪表无返回，发送错误消息
     */
    static public String packYBSJ(int no,double currentTotal,double flowSpeed,double beltSpeed,byte shellOpen,int type){
        StringBuilder sb= new StringBuilder();//no设为-1，未从仪表读到数据的空数据。
        String time = DateUtil.get();
        sb.append("KJ;YBSJ").append("|").append(no).append(";").append(currentTotal).append(";").append(flowSpeed).append(";");
        sb.append(beltSpeed).append(";").append(shellOpen).append(";").append(type).append(";").append(time).append("~||");
        return sb.toString();
    }
    
    public static Map depackYBSJ(String msg){
        Map map = new HashMap();
         msg = msg.substring(8);
        String[] records = msg.split("[;~|]");
        int no = Integer.parseInt(records[0]);
        double currentTotal = Double.parseDouble(records[1]);
        double flowSpeed=Double.parseDouble(records[2]);
        double beltSpeed=Double.parseDouble(records[3]);
        byte shellOpen = Byte.parseByte(records[4]);
        int type = Integer.parseInt(records[5]);
        Date time=DateUtil.get(records[6]);
        map.put("no", no);
        map.put("currentTotal", currentTotal);
        map.put("flowSpeed", flowSpeed);
        map.put("beltSpeed", beltSpeed);
        map.put("shellOpen", shellOpen);
        map.put("type", type);
        map.put("time", time);
        return map;
    }
    
    static public String packCLSC(long id,String cityName,String countyName,String mineName,String time,String unit ,double output,int rate){
        StringBuilder sb= new StringBuilder();
        
        sb.append("KJ;CLSC").append("|").append(id).append(";").append(cityName).append(";")
               .append(countyName).append(";").append(mineName).append(";");
        sb.append(time).append(";").append(unit).append(";").append(output).append(";").append(rate).append("~||");        
        return sb.toString();
    }
    static public String packCLSC(DataOf5mBean data){
        StringBuilder sb= new StringBuilder();
      
        sb.append("KJ;CLSC").append("|").append(data.getId()).append(";").append(data.getCityName()).append(";")
               .append(data.getCountyName()).append(";").append(data.getMineName()).append(";");
        sb.append(data.getYear()).append("-").append(data.getMonth()).append("-").append(data.getDay());
        sb.append("/").append(data.getHour()).append(":").append(data.getMin()).append(";");
        sb .append("t").append(";").append(data.getSumof5min()).append(";").append(data.getRate()).append("~||");        
        return sb.toString();
    }
    static public String packCLSC(DataOf5mBean[] datas){
        StringBuilder sb= new StringBuilder();
        
        sb.append("KJ;CLSC").append("|");
        for(int i=0;i<datas.length;i++){
            sb .append(datas[i].getId()).append(";").append(datas[i].getCityName()).append(";")
               .append(datas[i].getCountyName()).append(";").append(datas[i].getMineName()).append(";");
            sb.append(datas[i].getYear()).append("-").append(datas[i].getMonth()).append("-").append(datas[i].getDay());
            sb.append("/").append(datas[i].getHour()).append(":").append(datas[i].getMin()).append(";");
            sb .append("t").append(";").append(datas[i].getSumof5min()).append(";").append(datas[i].getRate()).append("~");       
        }
        sb.append("||");
        return sb.toString();
    }
    
    static public String packCLYC(long id,String cityName,String countyName,String mineName,String time,String status){
        StringBuilder sb= new StringBuilder();
        sb.append("KJ;CLYC").append("|").append(id).append(";").append(cityName).append(";")
               .append(countyName).append(";").append(mineName).append(";");
        sb.append(time).append(";").append(status).append("~||");       
        return sb.toString();
    }
    
   static public String packCLCS(CLCSBean clcs){
       String time = ""+clcs.getYear()+"-"+clcs.getMonth()+"-"+clcs.getDay()+"/"+clcs.getHour()+":"+clcs.getMin();
       StringBuilder sb= new StringBuilder();
        sb.append("KJ;CLCS").append("|").append(clcs.getId()).append(";").append(clcs.getCityName()).append(";")
               .append(clcs.getCountyName()).append(";").append(clcs.getMineName()).append(";");
        sb.append(time).append(";").append(clcs.getYearPlan()).append(";").append(clcs.getMonthPlan()).append(";").append(clcs.getDayPlan()).append(";").append(clcs.getHourPlan()).append(";")
                .append(clcs.getRate()).append(";").append(clcs.getDeviceName()).append(";").append(clcs.getSecurityNo()).append(";").append(clcs.getYearlyCheckNo()).append("~||");       
        return sb.toString();
   }
    
    static public List<MineData> dePack(String data){
        List<MineData> list = new ArrayList<MineData>();
        
        MineData mineData = null;
        String head = data.substring(0,7);
        data = data.substring(8);
        String[] records = data.split("[~|]");
        for(String record:records){
            
            if("KJ;CLSC".equals(head)){
                mineData = new DataOf5mBean();
            }else if("KJ;CLCS".equals(head)){
                mineData = new CLCSBean();
            }else if("KJ;CLYC".equals(head)){
                mineData = new CLYCBean();
            }            
            String[] items = record.split(";");
            mineData.setId(Long.parseLong(items[0]));
            mineData.setCityName(items[1]);
            mineData.setCountyName(items[2]);
            mineData.setMineName(items[3]);
            String orgCode = findOrgCode(mineData.getCityName(),mineData.getCountyName(),mineData.getMineName());
            mineData.setMineCode(orgCode);
            String[] timeItems = items[4].split("[-/: ]");//注意空格，缺少将产生错误,解析时间
            mineData.setYear(Short.parseShort(timeItems[0]));
            mineData.setMonth(Byte.parseByte(timeItems[1]));
            mineData.setDay(Byte.parseByte(timeItems[2]));
            mineData.setHour(Byte.parseByte(timeItems[3]));
            mineData.setMin(Byte.parseByte(timeItems[4]));
            
            if("KJ;CLSC".equals(head)){
                DataOf5mBean dataOf5mBean=(DataOf5mBean)mineData;
                dataOf5mBean.setSumof5min(Double.parseDouble(items[6]));
                dataOf5mBean.setRate(Byte.parseByte(items[7]));
                
            }else if("KJ;CLCS".equals(head)){
                CLCSBean clcs = (CLCSBean)mineData;
                clcs.setYearPlan(Double.parseDouble(items[5]));
                clcs.setMonthPlan(Double.parseDouble(items[6]));
                clcs.setDayPlan(Double.parseDouble(items[7]));
                clcs.setHourPlan(Double.parseDouble(items[8]));
                clcs.setRate(Double.parseDouble(items[9]));
                clcs.setDeviceName(items[10]);
                clcs.setSecurityNo(items[11]);
                clcs.setYearlyCheckNo(items[12]);
                
                
            }else if("KJ;CLYC".equals(head)){
                CLYCBean cLYCBean  = (CLYCBean)mineData;
                cLYCBean.setStatus(items[5]);                
            }
            list.add(mineData);
        }
        return list;
    }

    private static String findOrgCode(String cityName, String countyName, String mineName) {
        try {
            return orgDao.getCode(mineName);
        } catch (Exception ex) {
            return null;
        }
    }

    
    public static String pack(QHSSSJBean qhsssj) {        
        if("1".equals(qhsssj.getYCBS())){
            StringBuilder sb = new StringBuilder();
            sb.append(qhsssj.getSMYC()).append(qhsssj.getJLYC()).append(qhsssj.getWKYC()).append(qhsssj.getTXYC()).append(0).append(0);
            return packCLYC(qhsssj.getId(),qhsssj.getSHI(),qhsssj.getXIAN(),qhsssj.getMKMC(),qhsssj.getCJSJ(),sb.toString());
        }else{
            DataOf5mBean data = new DataOf5mBean();
            data.setCityName(qhsssj.getSHI());
            data.setCountyName(qhsssj.getXIAN());
            data.setMineName(qhsssj.getMKMC());
            data.setSumof5min(qhsssj.getXSZL());
            data.setRate((byte)0);
            String time= qhsssj.getCJSJ();
            
            String[] timeItems = time.split("[-/:]");
            data.setYear(Short.parseShort(timeItems[0]));
            data.setMonth(Byte.parseByte(timeItems[1]));
            data.setDay(Byte.parseByte(timeItems[2]));
            data.setHour(Byte.parseByte(timeItems[3]));
            data.setMin(Byte.parseByte(timeItems[4]));            
            
            return packCLSC(data);
        }
    }
}
